﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Xceed.Silverlight.DataGrid
{
  internal class ScrollingLayout : LinearLayout, ITransitionLayout
  {
    public ScrollingLayout( ILayoutContainer panel, Orientation orientation, MergeInfo mergeInfo )
      : base( panel, mergeInfo.ScrollingItems, orientation )
    {
      m_mergeInfo = mergeInfo;
      m_newLayout = new LinearLayout( panel, mergeInfo.NewLayoutItems, orientation );
    }

    public ILayout NewLayout
    {
      get
      {
        return m_newLayout;
      }
    }

    public override void SetViewportPosition( ViewportPosition viewportPosition )
    {
      base.SetViewportPosition( viewportPosition );
      this.NewLayout.SetViewportPosition( viewportPosition );
    }

    protected override Size ArrangePanelCore( Size finalSize, LayoutParameters parameters )
    {
      if( m_firstArrange )
      {
        bool scrollForward = m_mergeInfo.ScrollForward;
        IEnumerable<ILayoutElement> scrollingItems = ( scrollForward ) ? this.Elements : this.Elements.Reverse();

        Rect? lastArrangeRect = null;
        Orientation orientation = this.Orientation;
        foreach( ILayoutElement element in scrollingItems )
        {
          if( element.LastArrangeRect == null && lastArrangeRect != null )
          {
            Size desiredSize = this.GetChildDesiredSizeCore( element );
            Rect rect = lastArrangeRect.Value.FlipRectOrientation(orientation);
            if( scrollForward )
            {
              rect.Y += rect.Height;
            }
            else
            {
              rect.Y -= desiredSize.Height;
            }
            rect.Height = desiredSize.Height;
            rect.Width = finalSize.Width;

            rect = rect.FlipRectOrientation( orientation );
            element.LastArrangeRect = rect;


            if( element is VirtualGroupContainer )
            {
              element.Arrange( new ArrangeParameters( rect, new LayoutParameters( null ) ) );
            }
          }
          lastArrangeRect = element.LastArrangeRect;
        }

        m_firstArrange = false;
      }

      return base.ArrangePanelCore( finalSize, parameters );
    }

    private MergeInfo m_mergeInfo;
    private bool m_firstArrange = true;
    private LinearLayout m_newLayout;
  }
}
